<template>
	<view>
		<!-- 自定义头部导航 -->
		<!-- <page-meta>
			<navigation-bar :title="'原材料收货'" front-color="#000000" background-color="#FBFBFF" fixed />
		</page-meta> -->
	<u--form labelPosition="left" :model="umodel" ref="uForm" class="form" labelWidth="200rpx"
			errorType="toast">
			<u-form-item label="供应商组" prop="partnerGroupingName" @click="openGroupSelect">
				<u--input v-model="umodel.partnerGroupingName" border="none" disabled disabledColor="#ffffff" inputAlign="right"
					placeholder="请选择供应商组"></u--input>
				<template #right>
					<u-icon name="arrow-right"></u-icon>
				</template>
			</u-form-item>
			<u-form-item label="供应商名称" prop="partnerName" @click="openPartnerSelect">
				<u--input v-model="umodel.partnerName" border="none" disabled disabledColor="#ffffff"
					inputAlign="right" placeholder="请选择供应商名称"></u--input>
				<template #right>
					<u-icon name="arrow-right"></u-icon>
				</template>
			</u-form-item>
			<u-form-item label="交货地" prop="destinationDescription"  >
			  <u--input v-model="umodel.destinationDescription"  inputAlign="right" clearable
			            border="none" placeholder="交货地"></u--input>
			</u-form-item>
			<u-line></u-line>
		</u--form>
		<!-- <button type="primary" @click="save" class="custom-style" :loading="posting">保 存</button> -->
		<button type="primary" @click="send" class="custom-style" :loading="posting">发 货</button>
		<u-picker :show="showGroup" :columns="groups" keyName="partnerGroupingName" immediateChange @cancel="showGroup = false"
			@confirm="groupSelect"></u-picker>
		<u-picker :show="showPartner" :columns="partners" keyName="partnerName" immediateChange
			@cancel="showPartner = false" @confirm="partnerSelect"></u-picker>
	</view>
</template>

<script setup lang="ts">
	import { computed, ref,reactive } from 'vue';
	import { PageResult } from "../../../types/type";
	import ScanBar from '@/components/scan-bar/index.vue';
	import { onShow, onPullDownRefresh } from '@dcloudio/uni-app';
	import { http } from "../../../utils/http";
	import { useUserStore } from '../../../stores/useUserStore';

	// 重置表单信息 & 光标自动blur
	const scanBarRef = ref();
	const ListViewHeight = ref(0);
	const messageShow = ref(false);
	const deleteIndex = ref(0);
	const showGroup = ref(false);
	const showPartner = ref(false);
	let groups = reactive([]);
	let partners = reactive([]);
	// 登录工作组信息
	//const loginWorkInfo = computed(() => JSON.parse(useUserStore().loginWorkInfo));
	//console.log(loginWorkInfo.value)
	const posting = ref(false);
	const exteriorDelivery = ref()
	const props = defineProps({
		result: {
			type: String,
		},
	});
	// 表单数据定义
	const umodel = reactive({
		partnerGroupingId: '',
		partnerGroupingName: '',
		partnerId: '',
		partnerCode: '',
		partnerName: '',
		destinationDescription: '',
		plantId: '',
		plantCode: '',
	});
	exteriorDelivery.value = JSON.parse(props.result)
	//已扫描集合
	const scannedList : any = ref([])
	onShow(async () => {
		ListViewHeight.value = uni.getSystemInfoSync().windowHeight - 160;
		console.log('ListViewHeight.value', ListViewHeight.value);
		getDetail()
		getGroups()
	})
	onPullDownRefresh(() => {
		scannedList.value = [];
		getDetail('refresh')
	});
	async function send() {
		if (!umodel.partnerGroupingName) {
			uni.$u.toast('请选择供应商组！');
			return;
		} else if (!umodel.partnerName) {
			uni.$u.toast('请选择供应商名称！');
			return;
		} 
		else if (!umodel.destinationDescription) {
			uni.$u.toast('请填写交货地！');
			return;
		}else {
		console.log("111",scannedList.value.id);
		scannedList.value.destinationDescription=umodel.destinationDescription;
		try {
			const result : PageResult<any> = await http({
				method: 'POST',
				url: '/mes/mesexteriordelivery/delivery',
				data: {
					
						ids: [scannedList.value.id],
						supplierId:umodel.partnerId,
						supplierCode:umodel.partnerCode,
						destinationDescription:umodel.destinationDescription,
						plantId:umodel.plantId,
						plantCode:umodel.plantCode,
					
				}
			});
			if (result.code === 200) {
				uni.$u.toast('发货成功！');
				return;
			}
		} catch (e) {
			console.log(e);
		}
		}
	}
	async function save() {
		if (!umodel.partnerGroupingName) {
			uni.$u.toast('请选择供应商组！');
			return;
		} else if (!umodel.partnerName) {
			uni.$u.toast('请选择供应商名称！');
			return;
		} 
		else if (!umodel.destinationDescription) {
			uni.$u.toast('请填写交货地！');
			return;
		}else {
		try {
			const result : PageResult<any> = await http({
				method: 'POST',
				url: '/mes/mesexteriordelivery/saveone',
				data: {
					
						id:scannedList.value.id,
						supplierId:umodel.partnerId,
						supplierCode:umodel.partnerCode,
						destinationDescription:umodel.destinationDescription,
						plantId:umodel.plantId,
						plantCode:umodel.plantCode,
				}
			});
			if (result.code === 200) {
				uni.$u.toast('保存成功！');
				return;
			}
		} catch (e) {
			console.log(e);
		}
		}
	}
	async function getGroups() {
		try {
			const data = await http({
				method: 'POST',
				url: '/mdm/mdmpartnergrouping/getList',
				data: {					
				}
			});
			groups[0] = data.data;
		} catch (e) {
			console.log(e);
		}
	}
	async function getPartners() {
		try {
			const data = await http({
				method: 'GET',
				url: `/mdm/mdmpartner/queryByGroupId/${umodel.partnerGroupingId}`,
			});
			partners[0] = data.data;
		} catch (e) {
			console.log(e);
		}
	}
	// 获取扫码结果
	async function getScanResult(result : string) {
		if (!result) {
			return
		}
		console.log('扫码结果', result);
		getDetail(null, result)
	}

	//查询明细信息
	async function getDetail(type ?: string, result ?: string) {
		const res = await http({
			method: 'GET',
			url: `/mes/mesexteriordelivery/get/${exteriorDelivery.value.id}`,
		});
		console.log(res.data)
		scannedList.value = res.data;
		type === 'refresh' && uni.stopPullDownRefresh();
		console.log(111,scannedList.value.supplierId);
		if(scannedList.value.supplierId==null|| scannedList.value.supplierId === "")
		{
			getGroups()
		}
		else
		{
		getPartner();
		}
		umodel.destinationDescription=scannedList.value.destinationDescription;
		umodel.plantId=scannedList.value.mesProduceTransferFormDTOS[0].plantId;
		umodel.plantCode=scannedList.value.mesProduceTransferFormDTOS[0].plantCode;
	}
	//查询供应商信息
	async function getPartner() {
		const res = await http({
			method: 'GET',
			url: `/mdm/mdmpartner/get/${scannedList.value.supplierId}`,
		});
		console.log("222",res.data)
		umodel.partnerId=scannedList.value.supplierId;
		umodel.partnerCode=res.data.partnerCode;
		umodel.partnerName=res.data.partnerName;
		umodel.partnerGroupingId=res.data.partnerGroupingId;
		umodel.partnerGroupingName=res.data.partnerGroupingName;
		getPartners()
		
	}
	async function messageConfirm() {
		messageShow.value = false;
		scannedList.value.splice(deleteIndex.value, 1);
		deleteById()
	}
function openGroupSelect() {
		showGroup.value = true;
		uni.hideKeyboard();
	}

	function openPartnerSelect() {
		showPartner.value = true;
		uni.hideKeyboard();
	}
	function groupSelect(e) {
		if (e.value.length) {
			umodel.partnerGroupingId = e.value[0].id;
			umodel.partnerGroupingName = e.value[0].partnerGroupingName;
		}
		showGroup.value = false;
		umodel.partnerId = "";
		umodel.partnerCode = "";
		umodel.partnerName = "";
		getPartners();
	}
	
	function partnerSelect(e) {
		if (e.value.length) {
			umodel.partnerId = e.value[0].id;
			umodel.partnerCode = e.value[0].partnerCode;
			umodel.partnerName = e.value[0].partnerName;
			scannedList.value.supplierId=umodel.partnerId;
			scannedList.value.supplierCode=umodel.partnerCode;
			umodel.destinationDescription = e.value[0].country+e.value[0].province+e.value[0].city+e.value[0].region+e.value[0].street+e.value[0].houseCode+e.value[0].zipCode;
		}
		showPartner.value = false;
	}
	function messageCancel() {
		messageShow.value = false;
	}
	const deliveryUnit = ref()
	function removeDetail(item : any, index : any) {
		deleteIndex.value = index;
		messageShow.value = true;
		deliveryUnit.value = item;

	}
	async function deleteById() {
		await http({
			method: 'POST',
			url: `/mes/mesexteriordeliveryunit/delete`,
			data: { ids: [deliveryUnit.value.id] }
		}).then((res) => {
			if (res.code == "200") {
				uni.$u.toast('删除成功！');
			}
		});
	}
</script>

<style lang="scss" scoped>
	.page {
		display: flex;
		flex-direction: column;
	}
	.form {
		padding: $uni-page-padding;
	}
.custom-style {
			// width: 120px;
			margin: 25px 20px 20px;
			height: 40px;
			border-radius: 80px;

			&:nth-child(1) {
				flex: 1
			}

			&:nth-child(2) {
				flex: 3
			}
		}
	.upload-scan {
		padding: 10px 40rpx 10px 40rpx;
	}

	.area-header {
		color: #909193;
		padding: 10px 15px;
		margin-bottom: 0;
		font-size: 15px;
		background-color: #f3f4f6;
		display: flex;
	}

	.list-content {
		padding: $uni-page-padding;

		.list {
			.card {
				margin-top: 5px;
				margin-bottom: 10px;
				border-radius: 7px;
				display: flex;
				align-items: center;
				padding: 10px 0;
				transition: all 300ms ease;
				box-shadow: 0 0 1px rgba(0, 0, 0, 0.1);

				.left {
					// flex-basis: 90rpx;
					// padding-right: 10px;
					text-align: right;
					font-size: 13px;

					.enter {
						line-height: 20px;
						margin-bottom: 5px;
					}

					.number {
						font-size: 13px;
						line-height: 18px;
					}

					:deep(.u-icon--right) {
						flex-direction: row-reverse;
					}
				}

				.right {
					flex: 1;
					padding-left: 15px;

					.title {
						font-size: 18px;
						line-height: 20px;
						margin-bottom: 5px;
						font-weight: 700;
					}

					.body {
						font-size: 16px;
						line-height: 18px;

						.span {
							width: 50%;
							// text-align: left;
							display: inline-block;
						}
					}
				}
			}
		}
	}

	.search {
		display: flex;
		flex: 1;
	}
</style>